let ManagerPojo = require("../models/ManagerPojo")
let ManagerDao = require("../dao/managerDao")
let AllTasksDao = require("../dao/allTasksDao")
let UserDao = require("../dao/userDao")
let UserTasksDao = require("../dao/userTasksDao")
let AllTasksPojo = require("../models/AllTasksPojo")
let JsonMessage = require("../models/JsonMessage")
module.exports = {
    createNewtask: function (req, res) {
        let allTasksPojo = new AllTasksPojo(null, req.body.title, req.body.catagory, req.body.description, req.body.due_date, req.body.marks)
        AllTasksDao.insertTask(allTasksPojo).then((results) => {
            if (results.affectedRows > 0) {
                res.json(new JsonMessage("1006", null, "Create Task Success"))
            } else {
                res.json(new JsonMessage("4006", null, "Create Task Failure"))
            }
        });
    },
    assignTaskToUser: function (req, res) {

        UserTasksDao.insertUserTasks(req.body.userid, req.body.taskid, req, res).then(
            (results) => {
                if (results.affectedRows > 0) {
                    res.json(new JsonMessage("1011", null, `Task:${req.body.taskid} Asign To User:${req.body.userid} Success`))
                } else {
                    res.json(new JsonMessage("4011", null, `unknown error`))
                }
            },
            (error) => {
            console.log(error)
            res.json(new JsonMessage("4012", null, `Task:${req.body.taskid} Asign To User:${req.body.userid} Failure,it had assign`))
        })
    },
    modifyManagerAccount: function (req, res) {
        let managerModels = new ManagerPojo()
        managerModels.id = req.body.id,
            managerModels.fammily_name = req.body.fammily_name,
            managerModels.given_name = req.body.given_name,
            managerModels.sign_up_time = null,
            managerModels.email = req.body.email,
            managerModels.password = req.body.password

        ManagerDao.updateManager(managerModels).then((results => {
            res.json(new JsonMessage("1004", null, "Modify Manager Account Success"))
        }))
    },
    catManagerAccount: function (req, res) {
        ManagerDao.selectManager(req.session.userid).then((results => {
            res.json(new JsonMessage("1003", results, "Manager Account Information"))
        }))
    },
    deleteATask: function (req, res) {
        AllTasksDao.deleteATask(req.body.id).then((results) => {
            if (results.affectedRows > 0) {
                res.json(new JsonMessage("1008", results, "Delete Success"))
            } else {
                res.json(new JsonMessage("1003", null, "Delete Failfure"))
            }
        })
    },
    selectTodayTasks: function (req, res) {
        AllTasksDao.selectTodayTasks().then((results) => {
            if (results.length > 0) {
                res.json(new JsonMessage("1002", results, "今日任务"))
            } else {
                res.json(new JsonMessage("1003", null, "无今日任务"))
            }
        })
    },
    selectTodayTasksEscTime: function (req, res) {
        AllTasksDao.selectTodayTasksEscTime().then((results) => {
            if (results.length > 0) {
                res.json(new JsonMessage("1002", results, "今日任务"))
            } else {
                res.json(new JsonMessage("1003", null, "无今日任务"))
            }
        })
    },   selectTodayTasksDescTime: function (req, res) {
        AllTasksDao.selectTodayTasksDescTime().then((results) => {
            if (results.length > 0) {
                res.json(new JsonMessage("1002", results, "今日任务"))
            } else {
                res.json(new JsonMessage("1003", null, "无今日任务"))
            }
        })
    },
    selectAllTasksEscTime: function (req, res) {
        AllTasksDao.selectAllTasksEscTime().then((results) => {
            if (results.length > 0) {
                res.json(new JsonMessage("1002", results, "今日任务"))
            } else {
                res.json(new JsonMessage("1003", null, "无今日任务"))
            }
        })
    },
    selectAllTasksDescTime: function (req, res) {
        AllTasksDao.selectAllTasksDescTime().then((results) => {
            if (results.length > 0) {
                res.json(new JsonMessage("1002", results, "今日任务"))
            } else {
                res.json(new JsonMessage("1003", null, "无今日任务"))
            }
        })
    },
    selectTommorrowTasks: function (req, res) {
        AllTasksDao.selectTommorrowTasks().then((results) => {
            if (results.length > 0) {
                res.json(new JsonMessage("1004", results, "无明日任务"))
            } else {
                res.json(new JsonMessage("1005", null, "无明日任务"))
            }
        })
    },
    selectTommorrowTasksEscTime: function (req, res) {
        AllTasksDao.selectTommorrowTasksEscTime().then((results) => {
            if (results.length > 0) {
                res.json(new JsonMessage("1004", results, "无明日任务"))
            } else {
                res.json(new JsonMessage("1005", null, "无明日任务"))
            }
        })
    },
    selectTommorrowTaskDescTime: function (req, res) {
        AllTasksDao.selectTommorrowTaskDescTime().then((results) => {
            if (results.length > 0) {
                res.json(new JsonMessage("1004", results, "无明日任务"))
            } else {
                res.json(new JsonMessage("1005", null, "无明日任务"))
            }
        })
    },
    cancelTaskOfUser: function (req, res) {
        UserTasksDao.deleteUserTask(req.body.userid,req.body.taskid).then((results) => {
            if (results.affectedRows>0) {
                res.json(new JsonMessage("10012", results, "Delete User Task Success"))
            } else {
                res.json(new JsonMessage("1003", null, "Delete User Task Failure"))
            }
        })
    },

    selectAllTasks: function (req, res) {
        AllTasksDao.selectAllTasks().then((results) => {
            if (results.length > 0) {
                res.json(new JsonMessage("1006", results, "All Tasks"))
            } else {
                res.json(new JsonMessage("1007", null, "No Any Tasks"))
            }
        })
    },
    selectAllUsers: function (req, res) {
        UserDao.selectAllUsers().then((results) => {
            if (results.length > 0) {
                res.json(new JsonMessage("1009", results, "All Users"))
            } else {
                res.json(new JsonMessage("1010", null, "No Any Users"))
            }
        })
    },


}